package com.oocl.tm.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import com.oocl.eventListener.RadioButtonListener;
import com.oocl.eventListener.ShakeButtonListener;
import com.oocl.friendListDB.FriendList;
import com.oocl.msgProcess.ShakeProcess;
import com.oocl.service.ClientListener;
import com.oocl.service.SendThread;
import com.oocl.tm.protocol.MsgPackage;
import com.oocl.tm.protocol.Operation;


public class ChatFrame extends JFrame implements ActionListener{
	
	private Socket socket;
	private String  name;
	private ObjectOutputStream out;
	private ObjectInputStream in;
	
	private JTextArea chatTa;
	private JTextField sendTf;
	private JButton sendBtn;
	private JButton shakeBtn;
	private String receiver;
	
	private JPanel panRight;
	private ButtonGroup btnGroup;;
	
	ArrayList<String> friends;
	public static boolean flag;           //上下线标志位
	
	public ChatFrame(final Socket socket, final String name, final ObjectOutputStream out, final ObjectInputStream in, JFrame jframe) {
		
		jframe.dispose();
		
		this.friends = FriendList.getFriens();
		this.flag = true;
		this.socket = socket;
		this.name = name;
		this.out = out;
		this.in = in;
		this.receiver = "ALL";
		
		this.setSize(700, 400);
		this.setTitle(name + " to " + receiver);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setIconImage(new ImageIcon("logo.jpg").getImage());
		init();
		addEvent();
		this.setVisible(true);
		
		
		new ClientListener(socket,name,out,in,chatTa,this,panRight,btnGroup).start();          //监听服务器线程
		new ShakeProcess(this);
//		new SendThread(socket, name, out, in, chatTa).process();								//服务线程
		
		
	}
	
	private void init(){
		
		new FriendList();
		
		this.setLayout(new BorderLayout());
		JPanel panLeft=new JPanel(new BorderLayout());
		panRight=new JPanel(new GridLayout(7, 1,5,5));   
		JPanel panLeftBottom=new JPanel(new BorderLayout());
		JPanel panBtnGroup=new JPanel(new BorderLayout());
		JScrollPane scroll = new JScrollPane(panRight);
		
		panRight.setPreferredSize(new Dimension(110, 300));
		
		chatTa=new JTextArea(10,20);
		sendTf=new JTextField();
		sendBtn=new JButton("send");
		shakeBtn=new JButton("shake");
		btnGroup = new ButtonGroup();
		
		sendTf.setFont(new Font("瀹嬩綋", Font.BOLD, 18));
		sendTf.setForeground(Color.red);
		chatTa.setFont(new Font("瀹嬩綋", Font.BOLD, 16));
		chatTa.setEditable(false);
		
		panLeft.add(new JScrollPane(chatTa), BorderLayout.CENTER);
		panLeft.add(panLeftBottom,BorderLayout.SOUTH);
		panRight.setBorder(new LineBorder(Color.red));
		
		this.add(panLeft,BorderLayout.CENTER);
		this.add(scroll, BorderLayout.EAST);
		panBtnGroup.add(shakeBtn,BorderLayout.NORTH);
		panBtnGroup.add(sendBtn,BorderLayout.SOUTH);
		panLeftBottom.add(sendTf,BorderLayout.CENTER);
		panLeftBottom.add(panBtnGroup,BorderLayout.EAST);
		
		RadioButtonListener buttonListener = new RadioButtonListener(this, name);
		JRadioButton all = new JRadioButton("ALL", new ImageIcon("on.png"), true);
		all.setSize(50, 50);
		btnGroup.add(all);
		all.addActionListener(buttonListener);
		panRight.add(all);
		
		JRadioButton[] jrb = new JRadioButton[4];
		
//		String name = "Mike";
		//初始化好友列表
		int temp = 0;
		for (int i = 0; i < friends.size(); i++) {
			if(!name.equals(friends.get(i))){
				jrb[temp]= new JRadioButton(friends.get(i), new ImageIcon("off.png")); //////
				temp++;
			}
		}
		for (int i = 0; i < jrb.length; i++) {
			jrb[i].setSize(50,50);
		}
		for (int i = 0; i < jrb.length; i++) {
			btnGroup.add(jrb[i]);
		}	
		for (int i = 0; i < jrb.length; i++) {
			jrb[i].addActionListener(buttonListener);
		}
		for (int i = 0; i < jrb.length; i++) {
			panRight.add(jrb[i]);
		}
	}
	
	private void addEvent(){
		sendBtn.addActionListener(this);
		shakeBtn.addActionListener(new ShakeButtonListener(out, name, receiver, this));
		this.addWindowListener(new WindowAdapter() {
			public synchronized void windowClosing(WindowEvent e) {
				try {
					MsgPackage msg = new MsgPackage(Operation.EXIT, name, null, null, null, null);
					out.writeObject(msg);
					System.out.println("客户端 "+name + ":退出了客户端。。。");
					flag = false;
					synchronized (this) {
						out.close();
						in.close();
						socket.close();
					}
					System.exit(0);
				} catch (IOException e1) {
					System.out.println("客户端" + name + "已经推出。");
				}
			}
		});
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Calendar time = Calendar.getInstance();
		String content=sendTf.getText();
		if(!"".equals(content)){
			sendTf.setText("");
			System.out.println(name + " to " + receiver + ":" + content);
			chatTa.append(time.get(Calendar.HOUR_OF_DAY)+":"+time.get(Calendar.MINUTE)+ "  "+ name +" to " + receiver +  " :"+"\n" );
			chatTa.append("\t"+content+"\n");
			MsgPackage msg = new MsgPackage(Operation.CHAT, name, receiver, content, null, null);
			try {
				System.out.println("客户端" + name + "发送消息" + msg.toString());
				out.writeObject(msg);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
		}
		
	}



	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

}
